package com.scau.ums.service.imp.repair;

import com.scau.ums.mapper.CustomRepairreportMapper;
import com.scau.ums.mapper.RepairreportMapper;
import com.scau.ums.model.Repairreport;
import com.scau.ums.model.RepairreportExample;
import com.scau.ums.service.RepairReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserRepairReportImp implements RepairReport {


    private RepairreportMapper repairreportMapper;
    private CustomRepairreportMapper customMapper;

    @Autowired
    public UserRepairReportImp(RepairreportMapper repairreportMapper,CustomRepairreportMapper customMapper) {
        this.repairreportMapper=repairreportMapper;
        this.customMapper=customMapper;
    }

    @Override
    public Repairreport getRepairreportById(int id) {
        return repairreportMapper.selectByPrimaryKey(id);
    }

    public int updateRepairreport(Repairreport repairreport) { return repairreportMapper.updateByPrimaryKey(repairreport); }

    public List<Repairreport> getLatestRepairreportByType(int startIndex, int pageSize, int type) {
        return customMapper.selectRowByPageSetOrderByClause
                ("repairReport","date DESC",startIndex,pageSize,"type="+type);
    }

    public List<Repairreport> getLatestRepairreportByState(int startIndex,int pageSize, int state) {
        return customMapper.selectRowByPageSetOrderByClause
                ("repairReport","date DESC",startIndex,pageSize,"state="+state);
    }

    public List<Repairreport> getLatestRepairreportByReporterId(int startIndex,int pageSize,int reporterId)
    {
        return customMapper.selectRowByPageSetOrderByClause
                ("repairReport","date DESC",startIndex,pageSize,"reportorId="+reporterId);
    }

    public List<Repairreport> getUncheckedRepairreportByReporterId(int startIndex,int pageSize,String reporterId)
    {
        String whereClause=" reporterId="+reporterId+" and (state<6 or state=8)";
        return customMapper.selectRowByPageSetOrderByClause
            ("repairReport","date DESC",startIndex,pageSize,whereClause);
    }

    public List<Repairreport> getUnAccessRepairReportByState(int startIndex,int pageSize,String reporterId)
    {
        String whereClause=" reporterId="+reporterId+" and state=6";
        return customMapper.selectRowByPageSetOrderByClause
                ("repairReport","date DESC",startIndex,pageSize,whereClause);
    }

    //根据用户ID获取用户的历史报修记录
    public List<Repairreport> getAllRepairReportById(int startIndex,int pageSize,String reporterId){
        String whereClause=" reporterId="+reporterId;
        return customMapper.selectRowByPageSetOrderByClause("repairReport","date DESC",startIndex,pageSize,whereClause);
    }

    //根据报修单ID删除用户历史报修记录
    public int deleteHistoryRepairReportById(int repairReportId){
        return repairreportMapper.deleteByPrimaryKey(repairReportId);
    }

    public boolean newRepairReport(String reporterId,String reporterName,
                                   String reporterType,String telephone,String dormRegion,
                                   int dormBuilding,int dormRoom,String desc,
                                   int type,Date date,String mail,String remark){

        Repairreport repairreport=new Repairreport();

        repairreport.setId(0);
        repairreport.setReporterid(reporterId);
        repairreport.setReportername(reporterName);
        repairreport.setReportertype(reporterType);
        repairreport.setTelephone(telephone);
        repairreport.setDormregion(dormRegion);
        repairreport.setDormbuilding(dormBuilding);
        repairreport.setDormroom(dormRoom);
        repairreport.setRepairdesc(desc);
        repairreport.setType(type);
        repairreport.setDate(date);
        repairreport.setState(1);
        repairreport.setRemark(remark);
        repairreport.setMail(mail);
        return repairreportMapper.insert(repairreport)==1;

    }
}
